//
//  YXVisitView.swift
//  SinaWeiBo
//
//  Created by Vergil on 2017/5/9.
//  Copyright © 2017年 Vergil. All rights reserved.
//

import UIKit

class YXVisitView: UIView {
    
    //注册按钮
    lazy var registBtn: UIButton = UIButton(title: "注册",
                                            fontsize: 16,
                                            normalColor: UIColor.orange,
                                            highlightColor: UIColor.black,
                                            bgImageName: "common_button_white")
    //登录按钮
    lazy var loginBtn: UIButton = UIButton(title: "登录",
                                           normalColor: UIColor.darkGray,
                                           highlightColor: UIColor.black,
                                           bgImageName: "common_button_white")
    
    //设置访客信息视图
    //如果是首页  imageName == ""
    var visitView: [String: String]? {
        didSet {
            guard let imageName = visitView?["imageName"],
                let message = visitView?["message"] else {
                    return
            }
            
            tipLabel.text = message
            
            if imageName == "" {
                iconAnimate()
                return
            }
            
            iconView.image = UIImage(named: imageName)
            //其他界面隐藏小房子和遮罩
            smallHouse.isHidden = true
            fadeImage.isHidden = true
            
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("初始化失败")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    //私有控件
    //背后的圆圈
    fileprivate lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //上半部分半透明的遮盖
    fileprivate lazy var fadeImage:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //小房子
    fileprivate lazy var smallHouse:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //提示标签
    fileprivate lazy var tipLabel: UILabel = {
        let label = UILabel()
        label.text = "关注一些人,回这里看看会有什么惊喜 关注一些人,回这里看看会有什么惊喜"
        label.font = UIFont.systemFont(ofSize: 14)
        label.textColor = UIColor.darkGray
        label.numberOfLines = 0
        label.textAlignment = .center
        return label
    }()

}

extension YXVisitView {
    
    //转轮动画
    fileprivate func iconAnimate() {
        
        let anim = CABasicAnimation.init(keyPath: "transform.rotation")
        
        //动画值
        anim.toValue = 2 * Double.pi
        //重复次数
        anim.repeatCount = MAXFLOAT
        //持续时间
        anim.duration = 15
        
        //完成后是否删除
        anim.isRemovedOnCompletion = false
        
        iconView.layer.add(anim, forKey: nil)
    
    }
    
    fileprivate func setupUI() {
        
        //添加控件
        addSubview(iconView)
        addSubview(fadeImage)
        addSubview(smallHouse)
        addSubview(tipLabel)
        addSubview(registBtn)
        addSubview(loginBtn)
        
        //取消Autoresizing
        for v in self.subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //添加自动布局
        //背后的圆圈
        addConstraint(NSLayoutConstraint(item: iconView,
                                              attribute: .centerX,
                                              relatedBy: .equal,
                                              toItem: self,
                                              attribute: .centerX,
                                              multiplier: 1.0,
                                              constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView,
                                              attribute: .centerY,
                                              relatedBy: .equal,
                                              toItem: self,
                                              attribute: .centerY,
                                              multiplier: 1.0,
                                              constant: -60))
        //小房子
        addConstraint(NSLayoutConstraint(item: smallHouse,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: smallHouse,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: 0))
        //提示文字
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconView, 
                                         attribute: .centerX,
                                         multiplier: 1.0, 
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 20))
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .width, 
                                         relatedBy: .equal, 
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 236))
        //注册按钮
        addConstraint(NSLayoutConstraint(item: registBtn,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 100))
        addConstraint(NSLayoutConstraint(item: registBtn,
                                         attribute: .top,
                                         relatedBy: .equal, 
                                         toItem: tipLabel, 
                                         attribute: .bottom,
                                         multiplier: 1.0, 
                                         constant: 10))
        addConstraint(NSLayoutConstraint(item: registBtn,
                                         attribute: .left,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .left,
                                         multiplier: 1.0,
                                         constant: 0))
        //登录按钮
        addConstraint(NSLayoutConstraint(item: loginBtn,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 10))
        addConstraint(NSLayoutConstraint(item: loginBtn,
                                         attribute: .right,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .right,
                                         multiplier: 1.0,
                                         constant: 0))
        
        //vfl设置半透明图片
        let views = ["fadeImage" : fadeImage,
                     "loginBtn" : loginBtn] as [String : Any]
        let metrics = ["spacing" : -35]
        
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[fadeImage]-0-|",
                                                       options: [],//属性数组
                                                       metrics: nil,//映射-()-中()内的内容
                                                       views: views))//定义vfl中控件名称和实际控件名称映射关系
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[fadeImage]-(spacing)-[loginBtn]",
                                                      options: [],
                                                      metrics: metrics,//映射-()-中()内的内容
                                                      views: views))//定义vfl中控件名称和实际控件名称映射关系
        
        
    }
    
}
